#include "textprocess.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QPainter>
#include <QColorDialog>
#include <QTextList>
#include <QTextListFormat>

TextProcess::TextProcess(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Easy Word"));
    showWidget = new ShowWidget(this);
    setCentralWidget(showWidget);

    //设置字体
    fontLabel1 = new QLabel(tr("Font"));
    fontComboBox = new QFontComboBox;
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
    fontLabel2 = new QLabel(tr("Font Size"));
    sizeComboBox = new QComboBox;
    QFontDatabase db;
    foreach(int size, db.standardSizes())
        sizeComboBox->addItem(QString::number(size));
    boldBtn = new QToolButton;
    boldBtn->setIcon(QIcon("bold.png"));
    boldBtn->setCheckable(true);
    italicBtn = new QToolButton;
    italicBtn->setIcon(QIcon("italic.png"));
    italicBtn->setCheckable(true);
    underlineBtn = new QToolButton;
    underlineBtn->setIcon(QIcon("underline.png"));
    underlineBtn->setCheckable(true);
    colorBtn = new QToolButton;
    colorBtn->setIcon(QIcon("color.png"));
    colorBtn->setCheckable(true);

    //排序设置
    listLabel = new QLabel(tr("Sort"));
    listComboBox = new QComboBox;
    listComboBox->addItem("Standard");
    listComboBox->addItem("QTextListFormat::ListDisc");
    listComboBox->addItem("QTextListFormat::ListCircle");
    listComboBox->addItem("QTextListFormat::ListSquare");
    listComboBox->addItem("QTextListFormat::ListDecimal");
    listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    listComboBox->addItem("QTextListFormat::ListUpperAlpha");
    listComboBox->addItem("QTextListFormat::ListLowerRoman");
    listComboBox->addItem("QTextListFormat::ListUpperRoman");

    createActions();
    createMenus();
    createToolBars();


    connect(fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString)));
    connect(sizeComboBox, SIGNAL(activated(QString)), this, SLOT(ShowSizeSpinBox(QString)));
    connect(boldBtn, SIGNAL(clicked()), this, SLOT(ShowBoldBtn()));
    connect(italicBtn, SIGNAL(clicked()), this, SLOT(ShowUnderlineBtn()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(ShowColorBtn()));

    connect(listComboBox, SIGNAL(activated(int)), this, SLOT(ShowList(int)));

    connect(showWidget->text, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)), this, SLOT(ShowCurrentFormatChanged(const QTextCharFormat &)));

    connect(showWidget->text->document(), SIGNAL(undoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text->document(), SIGNAL(redoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text, SIGNAL(cursorPositionChanged()), this, SLOT(ShowCursorPoitionChanged()));
}

TextProcess::~TextProcess()
{

}

void TextProcess::createActions()
{
    //"打开"动作
    openFileAction = new QAction(QIcon("open.png"), tr("Open"), this);
    openFileAction->setShortcut(tr("Ctrl+O"));
    openFileAction->setStatusTip(tr("open the file."));
    connect(openFileAction, SIGNAL(triggered()), this, SLOT(ShowOpenFile()));

    //新建 动作
    NewFileAction = new QAction(QIcon("new.png"), tr("New"), this);
    NewFileAction->setShortcut(tr("Ctrl+N"));
    NewFileAction->setStatusTip(tr("create the new file."));
    connect(NewFileAction, SIGNAL(triggered()), this, SLOT(ShowNewFile()));


    //退出 动作
    exitAction = new QAction(QIcon("exit.png"), tr("Exit"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("exit this function."));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

    //复制 动作
    copyAction = new QAction(QIcon("copy.png"), tr("Copy"), this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("Copy this file."));

    //剪切 动作
    cutAction = new QAction(QIcon("cut.png"), tr("Cut"), this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("Cut this file."));

    //粘贴 动作
    pasteAction = new QAction(QIcon("paste.png"), tr("Paste"), this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("paste the file."));

    //关于 动作
    aboutAction = new QAction(QIcon("about.png"), tr("About"), this);
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(ShowAbout()));


    //打印文本 动作
    PrintTextAction = new QAction(QIcon("printText.png"), tr("PrintText"), this);
    PrintTextAction->setStatusTip(tr("print a text."));
    connect(PrintTextAction, SIGNAL(triggered()), this, SLOT(ShowPrintText()));

    //打印图形 动作
    PrintImageAction = new QAction(QIcon("printImage.png"), tr("PrintImage"), this);
    PrintImageAction->setStatusTip(tr("print a Image."));
    connect(PrintImageAction, SIGNAL(triggered()), this, SLOT(ShowPrintImage()));

    //放大 动作
    zoomInAction = new QAction(QIcon("zoomin.png"), tr("ZoomIn"), this);
    zoomInAction->setStatusTip(tr("zoomin the image."));
    connect(zoomInAction, SIGNAL(triggered()), this, SLOT(ShowZoomIn()));

    //缩小 动作
    zoomOutAction = new QAction(QIcon("zoomout.png"), tr("ZoomOut"), this);
    zoomOutAction->setStatusTip(tr("zoomout the image."));
    connect(zoomOutAction, SIGNAL(triggered()), this, SLOT(ShowZoomOut()));

    //实现图像旋转动作
    //旋转90
    rotate90Action = new QAction(QIcon("rotate90.png"), tr("Rotate90"), this);
    rotate90Action->setStatusTip(tr("rotate the image 90."));
    connect(rotate90Action, SIGNAL(triggered()), this, SLOT(ShowRotate90()));

    //旋转180
    rotate180Action = new QAction(QIcon("rotate180.png"), tr("Rotate180"), this);
    rotate180Action->setStatusTip(tr("rotate the image 180."));
    connect(rotate180Action, SIGNAL(triggered()), this, SLOT(ShowRotate180()));

    //旋转270
    rotate270Action = new QAction(QIcon("rotate270.png"), tr("Rotate270"), this);
    rotate270Action->setStatusTip(tr("rotate the image 270."));
    connect(rotate270Action, SIGNAL(triggered()), this, SLOT(ShowRotate270()));

    //实现镜像动作
    //纵向
    mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png"), tr("MirrorVertical"), this);
    mirrorVerticalAction->setStatusTip(tr("mirror vertical the image."));
    connect(mirrorVerticalAction, SIGNAL(triggered()), this, SLOT(ShowMirrorVertical()));

    //横向
    mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png"), tr("MirrorHorizontal"), this);
    mirrorHorizontalAction->setStatusTip(tr("mirror Horizontal the image."));
    connect(mirrorHorizontalAction, SIGNAL(triggered()), this, SLOT(ShowMirrorHorizontal()));

    //实现撤销和恢复动作
    //撤销
    undoAction = new QAction(QIcon("undo.png"), tr("Undo"), this);

    //恢复
    redoAction = new QAction(QIcon("redo.png"), tr("Redo"), this);

    //排序：左对齐、右对齐、居中、两端对齐
    actGrp = new QActionGroup(this);
    leftAction = new QAction(QIcon("left.png"), "Left", actGrp);
    leftAction->setCheckable(true);
    rightAction = new QAction(QIcon("right.png"), "Right", actGrp);
    rightAction->setCheckable(true);
    centerAction = new QAction(QIcon("center.png"), "Center", actGrp);
    centerAction->setCheckable(true);
    justifyAction = new QAction(QIcon("justify.png"), "Justify", actGrp);
    justifyAction->setCheckable(true);
    connect(actGrp, SIGNAL(triggered(QAction *)), this, SLOT(ShowAlignment(QAction *)));
}

void TextProcess::createMenus()
{
    //文件菜单
    fileMenu = menuBar()->addMenu(tr("File"));
    fileMenu->addAction(openFileAction);
    fileMenu->addAction(NewFileAction);
    fileMenu->addAction(PrintTextAction);
    fileMenu->addAction(PrintImageAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    //缩放菜单
    zoomMenu = menuBar()->addMenu(tr("Edit"));
    zoomMenu->addAction(copyAction);
    zoomMenu->addAction(cutAction);
    zoomMenu->addAction(pasteAction);
    zoomMenu->addAction(aboutAction);
    zoomMenu->addSeparator();
    zoomMenu->addAction(zoomInAction);
    zoomMenu->addAction(zoomOutAction);

    //旋转菜单
    rotateMenu = menuBar()->addMenu(tr("Rotate"));
    rotateMenu->addAction(rotate90Action);
    rotateMenu->addAction(rotate180Action);
    rotateMenu->addAction(rotate270Action);

    //镜像菜单
    mirrorMenu = menuBar()->addMenu(tr("Mirror"));
    mirrorMenu->addAction(mirrorVerticalAction);
    mirrorMenu->addAction(mirrorHorizontalAction);
}

void TextProcess::createToolBars()
{
    //文件工具条
    fileTool = addToolBar("File");
    fileTool->addAction(openFileAction);
    fileTool->addAction(NewFileAction);
    fileTool->addAction(PrintTextAction);
    fileTool->addAction(PrintImageAction);

    //编辑工具条
    zoomTool = addToolBar("Edit");
    zoomTool->addAction(copyAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(pasteAction);
    zoomTool->addSeparator();
    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction);

    //旋转工具条
    rotateTool = addToolBar("Rotate");
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action);

    //撤销和重做工具条
    doToolBar = addToolBar("doEdit");
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction);

    //字体工具条
    fontToolBar = addToolBar("Font");
    fontToolBar->addWidget(fontLabel1);
    fontToolBar->addWidget(fontComboBox);
    fontToolBar->addWidget(fontLabel2);
    fontToolBar->addWidget(sizeComboBox);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(boldBtn);
    fontToolBar->addWidget(italicBtn);
    fontToolBar->addWidget(underlineBtn);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(colorBtn);

    //排序工具条
    listToolBar = addToolBar("List");
    listToolBar->addWidget(listLabel);
    listToolBar->addWidget(listComboBox);
    listToolBar->addSeparator();
    listToolBar->addActions(actGrp->actions());

}

void TextProcess::ShowNewFile()
{
    //新建文件相关操作
}

void TextProcess::ShowAbout()
{
    //显示关于 Qt 消息框
}

void TextProcess::ShowOpenFile()
{
    //打卡文件操作
}

void TextProcess::loadFile(QString filename)
{
    //读取文件信息操作
}

void TextProcess::ShowPrintText()
{
    //打印文本信息操作
}

void TextProcess::ShowPrintImage()
{
    //打印图像操作，打开打印机对话框
}

void TextProcess::ShowZoomIn()
{
    //图像放大操作
}

void TextProcess::ShowZoomOut()
{
    //图像缩小操作
}

void TextProcess::ShowRotate90()
{
    //图像旋转90度操作
}

void TextProcess::ShowRotate180()
{
    //图像旋转180度操作
}

void TextProcess::ShowRotate270()
{
    //图像旋转270度操作
}

void TextProcess::ShowMirrorVertical()
{
    //图像横向镜像操作
}

void TextProcess::ShowMirrorHorizontal()
{
    //图像垂直镜像操作
}

void TextProcess::ShowFontComboBox(QString comboStr)
{
    //字体修改相关操作
}

void TextProcess::mergeFormat(QTextCharFormat format)
{
    //光标选择文本部分操作
}

void TextProcess::ShowSizeSpinBox(QString spinValue)
{
    //字体大小选择响应操作
}

void TextProcess::ShowBoldBtn()
{
       //字体加粗操作
}

void TextProcess::ShowItalicBtn()
{
    //字体斜体操作
}

void TextProcess::ShowUnderlineBtn()
{
    //添加字体下划线操作
}

void TextProcess::ShowColorBtn()
{
    //设置字体颜色

}

void TextProcess::ShowCurrentFormatChanged(const QTextCharFormat &fmt)
{
    //设置字符格式

}

void TextProcess::ShowAlignment(QAction *act)
{
    //文本对齐操作
}

void TextProcess::ShowCursorPoitionChanged()
{
    //光标位置改变槽函数
}

void TextProcess::ShowList(int index)
{
}
